package com.micro.ai.auth.controller;

import com.micro.ai.auth.entity.MenuI18n;
import com.micro.ai.auth.service.MenuService;
import com.micro.ai.commons.domain.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.micro.ai.auth.annotation.AuditLogAnnotation;
import java.util.List;

/**
 * 菜单国际化管理控制器
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/api/menus/i18n")
@Tag(name = "菜单国际化管理", description = "菜单多语言内容管理")
public class MenuI18nController {

    @Autowired
    private MenuService menuService;

    /**
     * 获取菜单的国际化信息
     */
    @GetMapping("/{menuId}")
    @PreAuthorize("hasAuthority('menu:view')")
    @Operation(summary = "获取菜单国际化信息", description = "获取指定菜单的所有语言版本")
    public ApiResponse<List<MenuI18n>> getMenuI18n(
            @Parameter(description = "菜单ID", required = true) @PathVariable String menuId) {
        log.info("获取菜单国际化信息: menuId={}", menuId);
        List<MenuI18n> i18nList = menuService.getMenuI18n(menuId);
        return ApiResponse.success(i18nList);
    }

    /**
     * 获取菜单的指定语言版本
     */
    @GetMapping("/{menuId}/{locale}")
    @PreAuthorize("hasAuthority('menu:view')")
    @Operation(summary = "获取菜单指定语言版本", description = "获取菜单的指定语言版本")
    public ApiResponse<MenuI18n> getMenuI18nByLocale(
            @Parameter(description = "菜单ID", required = true) @PathVariable String menuId,
            @Parameter(description = "语言代码", required = true) @PathVariable String locale) {
        log.info("获取菜单指定语言版本: menuId={}, locale={}", menuId, locale);
        MenuI18n i18n = menuService.getMenuI18nByLocale(menuId, locale);
        return ApiResponse.success(i18n);
    }

    /**
     * 创建或更新菜单国际化信息
     */
    @PostMapping("/{menuId}")
    @PreAuthorize("hasAuthority('menu:update')")
    @Operation(summary = "创建或更新菜单国际化信息", description = "创建或更新菜单的多语言内容")
    @AuditLogAnnotation(action = "UPDATE", resourceType = "MENU", description = "更新菜单国际化信息")
    public ApiResponse<Void> saveMenuI18n(
            @Parameter(description = "菜单ID", required = true) @PathVariable String menuId,
            @RequestBody List<MenuI18n> i18nList) {
        log.info("保存菜单国际化信息: menuId={}, locales={}", menuId, i18nList.size());
        menuService.saveMenuI18n(menuId, i18nList);
        return ApiResponse.success();
    }

    /**
     * 删除菜单的国际化信息
     */
    @DeleteMapping("/{menuId}")
    @PreAuthorize("hasAuthority('menu:delete')")
    @Operation(summary = "删除菜单国际化信息", description = "删除菜单的所有语言版本")
    @AuditLogAnnotation(action = "DELETE", resourceType = "MENU", description = "删除菜单国际化信息")
    public ApiResponse<Void> deleteMenuI18n(
            @Parameter(description = "菜单ID", required = true) @PathVariable String menuId) {
        log.info("删除菜单国际化信息: menuId={}", menuId);
        menuService.deleteMenuI18n(menuId);
        return ApiResponse.success();
    }

    /**
     * 删除菜单的指定语言版本
     */
    @DeleteMapping("/{menuId}/{locale}")
    @PreAuthorize("hasAuthority('menu:delete')")
    @Operation(summary = "删除菜单指定语言版本", description = "删除菜单的指定语言版本")
    @AuditLogAnnotation(action = "DELETE", resourceType = "MENU", description = "删除菜单指定语言版本")
    public ApiResponse<Void> deleteMenuI18nByLocale(
            @Parameter(description = "菜单ID", required = true) @PathVariable String menuId,
            @Parameter(description = "语言代码", required = true) @PathVariable String locale) {
        log.info("删除菜单指定语言版本: menuId={}, locale={}", menuId, locale);
        menuService.deleteMenuI18nByLocale(menuId, locale);
        return ApiResponse.success();
    }
}
